<template>
  <div ref="appRef" class="my-container-box">
    <search-top-form
      class="search-form"
      :search-form="searchObj.searchForm"
      :search-item="searchObj.searchItem"
      @handle-search="handleSearch"
      @handle-reset="handleReset"
      @re-count-table-height="getTableHeight"
    ></search-top-form>
    <table-page-content
      ref="tableRef"
      :loading="tableObj.loading"
      :table-data="tableObj.tableList"
      :table-headers="tableHeaders"
      :table-height="tableHeight"
      :page-config="searchObj.pageConfig"
      @page-size-change="pageSizeChange"
      @current-page-change="currentPageChange"
    >
      <template #operate="{ row }">
        <div class="table-btn-box">
          <el-button v-permission="{ action: 'find' }" link @click="handleDetail(row.financeId)"
            ><i class="iconfont icon-chakanxiangqing"></i> <span class="text">查看详情</span></el-button
          >
        </div>
      </template>
    </table-page-content>
    <detail-component ref="detailRef"></detail-component>
  </div>
</template>

<script lang="ts" setup name="Finance">
import { useProxy } from '@/utils/proxy'
import { useTableHeight } from '@/hooks/tableHeight'
import { pageConfig } from '@/utils/config'
import { tableHeaders, searchItem } from './config'
import { getTabList } from './api'
import detailComponent from './components/detail.vue'
const { appRef, getTableHeight, tableHeight } = useTableHeight()
const { proxy } = useProxy()
const tableRef = ref()
const detailRef = ref()
// 搜索对象
const searchObj = reactive({
  searchForm: {
    years: null,
    enterpriseName: null,
    areaCode: null
  },
  pageConfig: { ...pageConfig },
  searchItem
})
const tableObj = reactive({
  loading: false,
  tableList: [
    { id: 1, enterpriseName: '测试' },
    { id: 2, enterpriseName: '测试2' }
  ]
})
// 查看详情
const handleDetail = (id: string) => {
  detailRef.value.state.currentId = id
  detailRef.value.getList()
  detailRef.value.state.show = true
}
// 搜索
const handleSearch = () => {
  searchObj.pageConfig.pageIndex = 1
  getList()
}
const handleReset = () => {
  searchObj.searchForm = {
    years: null,
    enterpriseName: null,
    areaCode: null
  }
  handleSearch()
}
// 获取列表数据
const getList = () => {
  tableObj.loading = true
  tableObj.tableList = []
  getTabList<any>({
    ...searchObj.searchForm,
    ...searchObj.pageConfig
  })
    .then((res) => {
      tableObj.tableList = res.data
      searchObj.pageConfig.total = res.total
      tableObj.loading = false
    })
    .catch(() => {
      tableObj.loading = false
    })
}
// 表格切换page-size
const pageSizeChange = (val: number) => {
  searchObj.pageConfig.pageSize = val
  searchObj.pageConfig.pageIndex = 1
  getList()
}
// 切换分页
const currentPageChange = (val: number) => {
  searchObj.pageConfig.pageIndex = val
  getList()
}
const init = () => {
  getList()
}
init()
</script>

<style lang="scss" scoped></style>
